// 创建 socket 连接
ws = new WebSocket("ws://127.0.0.1:7272");

// 连接socket时 触发
ws.onopen = function() {
    // 登录
    var login_data = '{"type":"init","room":"'+room+'","uid":"'+uid+'","username":"'+username+'"}';
    console.log(login_data);
    ws.send(login_data);
};

ws.onmessage = function (res) {
    var data = eval("("+res.data+")");
    switch (data['message_type']){
        case 'ping':
            console.log(111);
            break;
        case "init":
            // 进入房间提示
            showMessage('login',data);
            break;
        case "mess":
            showMessage('mess',data);
            break;
        case "sendGift":
            console.log(data);
            if (client=='normal1' && data['ecuid']==uid) {
                   $('#m').html(data['mb']);
            }
            showMessage('sendgift',data);
            gift.addgiftToQueue(data,false);
            break;
    }
}

var sendMsg = document.getElementById('sub');
sendMsg.onclick = function () {
    var message = document.getElementById('mess');
    mes = message.value;
    if (mes == '') {
         return;
    }
    // 判断是否被禁言
    adata = {room:room};
    $.ajax({
        type:"POST",
        url:"/live/is-roomgag",
        data:adata,
        dataType:"json",
        success:function (res) {
            if (res['code']==200){
                data = '{"type":"mess","room":"'+room+'","username":"'+username+'","uid":"'+uid+'","mess":"'+mes+'"}';
                ws.send(data);
                message.value = '';
            }else{
                showTips('你已被禁言，无法在本房间发言');
            }
        }
    });
}

/**
 * 弹幕消息显示
 * @param $type
 * @param $data
 */
function showMessage($type,data) {
    if ($type=='login') {
          $html = '<div class="dmbox_p">';
          if( data['utype']=='zhubo' ){
              $html += '<span class="u-t-zhubo">主播</span>';
          }
          $html +='<span class="u_name">'+data['username']+'</span> <span style="color: #666;">欢迎来到本直播间</span></div>'
    }else if ($type == 'mess'){
        if (data['jl']==1) {
             showTips('恭喜你，发弹幕触发了'+data['jlval']+'的积分奖励');
        }
        if (data['jl']==2) {
            showTips('恭喜你，发弹幕触发了'+data['jlval']+'的经验奖励');
        }
        $html = '<div class="dmbox_p">';
        if( data['utype']=='zhubo' ){
            $html += '<span class="u-t-zhubo">主播</span>';
        }
        if( data['isroomadmin']=='1' ){
            $html += '<span class="u-t-fg">房管</span>';
        }
        $html +='<span class="u_name"  onclick="showdmMenu(this)">'+data['username']+'';
        if (isZhubo==1 || isAdmin==1) {
            $html += '<div class="u_op"><b></b>';
            if (isZhubo==1){
                $html +=' <span onclick="roomAdmin(this,1)" uid="'+data['uid']+'">房管</span><span onclick="roomAdmin(this,0)" uid="'+data['uid']+'">禁言</span>'
            }
            if (isAdmin==1){
                $html += '<span onclick="roomAdmin(this,0)" uid="'+data['uid']+'">禁言</span>'
            }
            $html += '</div>'
        }
        $html +='</span> <span style="color: #666;">'+data['mess']+'</span></div>'
    }else if ($type == 'sendgift') {
        console.log(data);
        $html = '<div class="dmbox_p">';
        if( data['utype']=='zhubo' ){
            $html += '<span class="u-t-zhubo">主播</span>';
        }
        $html +='<span class="u_name">'+data['data']['username']+'</span> <span style="color: #666;">送了 '+data['num']+'个 '+data['data']['gname']+'</span>给主播</div>';
        if (client=='zhubo') {
             var gift_id = document.getElementById('gift_id_'+data['data']['gid']);
             var gift_span = gift_id.getElementsByTagName('span')[0];
             gift_span.innerHTML = parseInt(gift_span.innerHTML)+parseInt(data['num']);
        }
    }
    $('#danmbox').append($html);
    // 获取最后一个div
    var dm = document.getElementsByClassName('dmbox_p');
    var dmLast = dm[dm.length-1];
    dmLast.scrollIntoView(true);
    setTimeout(function () {
        dmshowAnimation();
    },30)
}

function dmshowAnimation() {
    var dump = document.getElementsByClassName('dmbox_p');
    for (var i=0; i<dump.length; i++) {
        dump[i].style.marginTop = 0;
    }
}